// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-11-04 15:15:27
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2025-01-02 17:19:02
 * @FilePath: /SGSim/Import/BDFImport/BulkData/PARAMETER/TSTEPParser.cpp
 */
#include "TSTEPParser.h"

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <map>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Parameter/TSTEPData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Parameter/ITSTEPService.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (TSTEP);
/*
// file://./../../Doc/MarkDown/BulkData/TSTEP/TSTEP.md>
*/

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::Import::BDF;
using namespace SG::DataStructure;
// 以下需要修改
#include <cstdint>

enum TSTEPIndex : std::uint8_t
{
    TSTEPIndex_N1  = 2,  //
    TSTEPIndex_DT1 = 3,  // 增量数量
    TSTEPIndex_NO1 = 4,  // 蠕变分析的增量时间间隔
};

namespace
{
    SG::DataStructure::Common::Status ParseTData (std::vector<std::string>& dividedEntryInfo, FEM::TSTEPData& tempData)
    {
        size_t RowNum = dividedEntryInfo.size () / 10;
        for (size_t curRowIndex = 0; curRowIndex < RowNum; curRowIndex++)
        {
            FEM::TSTEPData::TData tempTData = { 0 };
            tempTData.m_NO                  = 1;
            for (size_t fieldsColumn = 2; fieldsColumn < 5; fieldsColumn++)
            {
                // 计算当前字段的索引
                const size_t curIndex  = curRowIndex * 10 + fieldsColumn;
                std::string& curStrKey = dividedEntryInfo[curIndex];
                bool const   isEmpty   = is_all_space (curStrKey);
                if (isEmpty)
                {
                    continue;
                }
                SG::Import::BDF::covertToScientificNotation (curStrKey);
                switch (fieldsColumn)
                {
                case 2:
                    tempTData.m_N = convertToI64 (curStrKey);
                    break;
                case 3:
                    tempTData.m_DT = convertToReal (curStrKey);
                    break;
                case 4:
                    tempTData.m_NO = convertToI64 (curStrKey);
                    break;
                default:
                    break;
                }
            }
            tempData.m_TList.push_back (tempTData);
        }
        return SG::DataStructure::Common::Status::STATUS_Success;
    }

}  // namespace

SG::DataStructure::Common::Status BDF::BULKDATA::ParseTSTEP (const std::shared_ptr<DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                dividedEntryInfo)
{
    // 创建临时节点数据结构，默认值为0
    SG::DataStructure::FEM::TSTEPData tempData;

    // TODO(bug 001) m_dominId的正确填写
    tempData.m_domainId = 1;

    tempData.m_sId = convertToI64 (dividedEntryInfo[1]);
    ParseTData (dividedEntryInfo, tempData);

    // 将解析完成的节点数据添加到数据库服务中
    return dbServiceFactory->get<ITSTEPService> ()->Add (tempData);
}